package Protocol;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;

//CIPHER / GENERATORS
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.KeyGenerator;

//KEY SPECIFICATIONS
import java.security.spec.KeySpec;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEParameterSpec;

//EXCEPTIONS
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import java.io.UnsupportedEncodingException;
import java.io.IOException;

public class TestCommunicationProtocol {

	/**
	 * @param args
	 * @throws NoSuchAlgorithmException
	 * @throws BadPaddingException
	 * @throws IllegalBlockSizeException
	 * @throws UnsupportedEncodingException
	 * @throws InvalidAlgorithmParameterException
	 */
	public static void testCommunicationProtocol(String[] args) throws NoSuchAlgorithmException,
			UnsupportedEncodingException, IllegalBlockSizeException,
			BadPaddingException, InvalidAlgorithmParameterException {
		// generate an AES session key
		KeyGenerator keyGen = KeyGenerator.getInstance("AES");
		keyGen.init(128);
		SecretKey sessionSecKey = keyGen.generateKey();

		String username = "snaiqiao";
		CommunicationProtocol comPro = new CommunicationProtocol(sessionSecKey);
		// encry the username to obtain EncryptMessageType
		EncryptMessageType encrymsg = comPro.encryptMessage(username);
		// decry the EncryptMessageType to obtain the original string
		String encryDecrymsg = comPro.decryptMessage(encrymsg);
		// output the result to check if the protocol function correctlly
		System.out.println("the original msg is " + username);
		System.out.println("Encry and decry the message " + encryDecrymsg);

	}

}